package cn.laixueit.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.laixueit.base.R;
import cn.laixueit.pojo.DatadictionartDetail;
import cn.laixueit.service.DatadictionartDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.laixueit.pojo.Datadictionart;
import cn.laixueit.service.DatadictionartService;
import cn.laixueit.mapper.DatadictionartMapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class DatadictionartServiceImpl extends MPJBaseServiceImpl<DatadictionartMapper, Datadictionart>
    implements DatadictionartService{

    @Resource
    private DatadictionartDetailService datadictionartDetailService;


    @Override
    public Map<String, LinkedList<DatadictionartDetail>> getDataDictionArtMapList(String dataDictCodes) {
        List<Datadictionart> datadictionartList = this.getDataDictionArtList(dataDictCodes);
        if(CollectionUtil.isNotEmpty(datadictionartList)){
            Map<String, LinkedList<DatadictionartDetail>> map = datadictionartList.stream().collect(Collectors.toMap(Datadictionart::getCode, Datadictionart::getDatadictionartDetails));
            return map;
        }
        return new HashMap<>();
    }

    @Override
    public List<Datadictionart> getDataDictionArtList(String dataDictCodes) {
        List<String> dataCodes = new ArrayList<>();
        if(StringUtils.isNotBlank(dataDictCodes)){
            dataCodes = Arrays.asList(dataDictCodes.split(",")).stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
        }
        MPJLambdaWrapper<Datadictionart> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Datadictionart.class)
                .selectCollection(DatadictionartDetail.class,Datadictionart::getDatadictionartDetails)
                .leftJoin(DatadictionartDetail.class,DatadictionartDetail::getDatadictCode,Datadictionart::getCode)
                .in(CollectionUtil.isNotEmpty(dataCodes),Datadictionart::getCode,dataCodes);
        List<Datadictionart> datadictionartList = selectJoinList(Datadictionart.class, wrapper);
        return datadictionartList;
    }

    @Override
    public void saveDatadictionart(Datadictionart datadictionart) {
        String code = datadictionart.getCode();
        List<Datadictionart> list = list(lambdaQuery().eq(Datadictionart::getCode,code).getWrapper());
        if(CollectionUtil.isNotEmpty(list)){
            throw new RuntimeException("已存在相同编码的字典，请更换");
        }else{
            saveOrUpdate(datadictionart);
        }
    }

    @Override
    public R pageDatadictionart(Page<Datadictionart> pageInfo, Datadictionart datadictionart) {
        QueryWrapper<Datadictionart> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(datadictionart.getCode()),Datadictionart::getCode,datadictionart.getCode())
                .like(StringUtils.isNotBlank(datadictionart.getName()),Datadictionart::getName,datadictionart.getName());
        Page<Datadictionart> page = page(pageInfo, wrapper);
        return R.ok().put("rows",page.getRecords()).put("total",page.getTotal());
    }

    @Override
    public R getDataDictDetails(String dataDictCode) {
        List<DatadictionartDetail> detailList = datadictionartDetailService.list(datadictionartDetailService.lambdaQuery()
                .eq(DatadictionartDetail::getDatadictCode,dataDictCode)
                .orderByAsc(DatadictionartDetail::getSort)
                .getWrapper());
        return R.ok().put("rows",detailList);
    }

    @Override
    public void saveDatadictionartDetail(DatadictionartDetail datadictionartDetail) {
        String datadictCode = datadictionartDetail.getDatadictCode();
        if(ObjectUtil.isNotEmpty(datadictionartDetail.getSort())) {
            List<DatadictionartDetail> list = datadictionartDetailService.list(datadictionartDetailService.lambdaQuery()
                    .eq(DatadictionartDetail::getDatadictCode, datadictCode)
                    .orderByDesc(DatadictionartDetail::getSort)
                    .getWrapper());
            if (CollectionUtil.isNotEmpty(list)) {
                Integer sort = list.stream().findFirst().get().getSort();
                if (ObjectUtil.isEmpty(sort)) {
                    sort = list.size();
                }
                datadictionartDetail.setSort(sort + 1);
            }
        }
        datadictionartDetailService.saveOrUpdate(datadictionartDetail);
    }

    @Override
    public R deleteDatadictByCode(String dataDictCode) {
        int count = datadictionartDetailService.count(datadictionartDetailService.lambdaQuery().eq(DatadictionartDetail::getDatadictCode,dataDictCode).getWrapper());
        if(count>0){
            return R.error().put("msg","删除失败，存在子数据");
        }
        remove(lambdaQuery().eq(Datadictionart::getCode,dataDictCode).getWrapper());
        return R.ok();
    }

    @Override
    public R deleteDatadictDetailByCode(String dataDictCode) {
        datadictionartDetailService.remove(datadictionartDetailService.lambdaQuery().eq(DatadictionartDetail::getCode,dataDictCode).getWrapper());
        return R.ok();
    }
}




